<?php

namespace App\Admin\Controllers\Common;

use Carbon\Carbon;
use App\Models\StudentClass;
use App\Admin\Controllers\Common\RenewDataController;
use App\Admin\Controllers\Common\RenewStaticData;
use App\Models\InviteLog;

/**
 * 数据来源类
 */
class DataSource
{
    protected $bt = '';
    protected $et = '';

    public function getDataSource($bt, $et, $board, $config = [])
    {
        $this->bt = $bt;
        $this->et = $et;
        $this->config = $config;
        if ($board == 'channelInvite') {
            return $this->channelInvite();
        }
        return [];
    }

    public function channelInvite()
    {
        $log = InviteLog::with(['student', 'sales'])->get()->toArray();
        foreach ($log as &$lv) {
            $lv['sales_id'] = $lv['sales_id'];
            $lv['sales_name'] = $lv['sales']['name'] ?? '-';
            $lv['sales_name_total'] = '【'.$lv['sales_id'].'】' . $lv['sales_name'];
            $lv['invite'] = 
            $lv['age'] = 
            $lv['regist'] = '-';
            if ($lv['behave'] == 0) {
                $lv['invite'] = '是';
            } else if ($lv['behave'] == 1) {
                $lv['regist'] = '是';
            }
            $lv['date'] = date('Y-m-d', strtotime($lv['created_at']));
            $lv['phone'] = $lv['student']['phone'] ?? '-';
            if (isset($lv['student']['birthday'])) {
                $lv['age'] = Carbon::parse()->diffInYears($lv['student']['birthday']);
            }
            $lv['begin_control'] = $lv['student']['begin_control'] ?? '-';
            $lv['total'] = '是';
        }
        return $log;
    }
}
